﻿#region 头注释
/******************************************************************************
文件名称    :   Surgery.cs
创建日期    :   2011/5/11 22:42:59
功能描述    :   手术信实体类
修改日志    :   
    2011-12-13 22:42  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// 手术信实体类
    /// </summary>
    public class Surgery : EntityBase<Surgery, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public Surgery()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            surgeryDate = DateTime.MinValue;
            kidney = null;
            kidneyId = null;
            softTube = null;
            softTubeId = null;
            hardTube = null;
            hardTubeId = null;
            sperm = null;
            spermId = null;
            pg = null;
            pgId = null;
            kidneyPos = null;
            stoneSize = null;
            stonePosition = null;
            recordFile = null;
            recordFileId = null;
            infection = null;
            infectionId = null;
            stoneRemain = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override Surgery CreateNew()
        {
            return new Surgery();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.Surgery; }
        }

        /// <summary>
        /// 获取或设置手术时间
        /// </summary>
        public DateTime SurgeryDate
        {
            get { return surgeryDate; }
            set
            {
                if (entityState == EntityState.Saved && surgeryDate != value) entityState = EntityState.Changed;
                surgeryDate = value;
            }
        }
        protected DateTime surgeryDate;

        /// <summary>
        /// 获取或设置经皮肾镜
        /// </summary>
        public SurgeryKidney Kidney
        {
            get { return kidney; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != kidneyId) entityState = EntityState.Changed;
                kidney = value;
            }
        }
        protected SurgeryKidney kidney;

        /// <summary>
        /// 获取或设置经皮肾镜
        /// </summary>
        public long? KidneyId
        {
            get { return kidney == null ? kidneyId : kidney.Id; }
            set { kidneyId = value; }
        }
        protected long? kidneyId;

        /// <summary>
        /// 获取或设置输尿软管镜
        /// </summary>
        public SurgerySofttube SoftTube
        {
            get { return softTube; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != softTubeId) entityState = EntityState.Changed;
                softTube = value;
            }
        }
        protected SurgerySofttube softTube;

        /// <summary>
        /// 获取或设置输尿软管镜
        /// </summary>
        public long? SoftTubeId
        {
            get { return softTube == null ? softTubeId : softTube.Id; }
            set { softTubeId = value; }
        }
        protected long? softTubeId;

        /// <summary>
        /// 获取或设置输尿硬管镜
        /// </summary>
        public SurgeryHardtube HardTube
        {
            get { return hardTube; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != hardTubeId) entityState = EntityState.Changed;
                hardTube = value;
            }
        }
        protected SurgeryHardtube hardTube;

        /// <summary>
        /// 获取或设置输尿硬管镜
        /// </summary>
        public long? HardTubeId
        {
            get { return hardTube == null ? hardTubeId : hardTube.Id; }
            set { hardTubeId = value; }
        }
        protected long? hardTubeId;

        /// <summary>
        /// 获取或设置精囊镜
        /// </summary>
        public SurgerySperm Sperm
        {
            get { return sperm; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != spermId) entityState = EntityState.Changed;
                sperm = value;
            }
        }
        protected SurgerySperm sperm;

        /// <summary>
        /// 获取或设置精囊镜
        /// </summary>
        public long? SpermId
        {
            get { return sperm == null ? spermId : sperm.Id; }
            set { spermId = value; }
        }
        protected long? spermId;

        /// <summary>
        /// 获取或设置前列腺手术
        /// </summary>
        public SurgeryPG Pg
        {
            get { return pg; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != pgId) entityState = EntityState.Changed;
                pg = value;
            }
        }
        protected SurgeryPG pg;

        /// <summary>
        /// 获取或设置前列腺手术
        /// </summary>
        public long? PgId
        {
            get { return pg == null ? pgId : pg.Id; }
            set { pgId = value; }
        }
        protected long? pgId;

        /// <summary>
        /// 获取或设置肾的左右
        /// </summary>
        public int? KidneyPos
        {
            get { return kidneyPos; }
            set
            {
                if (entityState == EntityState.Saved && kidneyPos != value) entityState = EntityState.Changed;
                kidneyPos = value;
            }
        }
        protected int? kidneyPos;

        /// <summary>
        /// 获取或设置结石大小
        /// </summary>
        public decimal? StoneSize
        {
            get { return stoneSize; }
            set
            {
                if (entityState == EntityState.Saved && stoneSize != value) entityState = EntityState.Changed;
                stoneSize = value;
            }
        }
        protected decimal? stoneSize;

        /// <summary>
        /// 获取或设置结石位置
        /// </summary>
        public string StonePosition
        {
            get { return stonePosition; }
            set
            {
                if (entityState == EntityState.Saved && stonePosition != value) entityState = EntityState.Changed;
                stonePosition = value;
            }
        }
        protected string stonePosition;

        /// <summary>
        /// 获取或设置手术录像文件id
        /// </summary>
        public MediaFile RecordFile
        {
            get { return recordFile; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != recordFileId) entityState = EntityState.Changed;
                recordFile = value;
            }
        }
        protected MediaFile recordFile;

        /// <summary>
        /// 获取或设置手术录像文件id
        /// </summary>
        public long? RecordFileId
        {
            get { return recordFile == null ? recordFileId : recordFile.Id; }
            set { recordFileId = value; }
        }
        protected long? recordFileId;

        /// <summary>
        /// 获取或设置感染
        /// </summary>
        public SurgeryInfection Infection
        {
            get { return infection; }
            set
            {
                if (entityState == EntityState.Saved && value != null && value.Id != infectionId) entityState = EntityState.Changed;
                infection = value;
            }
        }
        protected SurgeryInfection infection;

        /// <summary>
        /// 获取或设置感染
        /// </summary>
        public long? InfectionId
        {
            get { return infection == null ? infectionId : infection.Id; }
            set { infectionId = value; }
        }
        protected long? infectionId;

        /// <summary>
        /// 获取或设置结石残留
        /// </summary>
        public string StoneRemain
        {
            get { return stoneRemain; }
            set
            {
                if (entityState == EntityState.Saved && stoneRemain != value) entityState = EntityState.Changed;
                stoneRemain = value;
            }
        }
        protected string stoneRemain;

        /// <summary>
        /// 获取或设置备注
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置更新时间戳
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override Surgery CopyTo(Surgery entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.surgeryDate = this.surgeryDate;
            entity.kidney = this.kidney;
            entity.kidneyId = this.kidneyId;
            entity.softTube = this.softTube;
            entity.softTubeId = this.softTubeId;
            entity.hardTube = this.hardTube;
            entity.hardTubeId = this.hardTubeId;
            entity.sperm = this.sperm;
            entity.spermId = this.spermId;
            entity.pg = this.pg;
            entity.pgId = this.pgId;
            entity.kidneyPos = this.kidneyPos;
            entity.stoneSize = this.stoneSize;
            entity.stonePosition = this.stonePosition;
            entity.recordFile = this.recordFile;
            entity.recordFileId = this.recordFileId;
            entity.infection = this.infection;
            entity.infectionId = this.infectionId;
            entity.stoneRemain = this.stoneRemain;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<Surgery>
        {
            Surgery entity;
            public DefaultXmlSerializer(Surgery entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public Surgery Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "SurgeryDate":
                        entity.surgeryDate = XmlConvert.ToDateTime(reader.ReadElementString(), XmlDateTimeSerializationMode.Utc);
                        break;
                    case "KidneyId":
                        entity.kidneyId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "SoftTubeId":
                        entity.softTubeId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "HardTubeId":
                        entity.hardTubeId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "SpermId":
                        entity.spermId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "PgId":
                        entity.pgId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "KidneyPos":
                        entity.kidneyPos = XmlConvert.ToInt32(reader.ReadElementString());
                        break;
                    case "StoneSize":
                        entity.stoneSize = XmlConvert.ToDecimal(reader.ReadElementString());
                        break;
                    case "StonePosition":
                        entity.stonePosition = reader.ReadElementString();
                        break;
                    case "RecordFileId":
                        entity.recordFileId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "InfectionId":
                        entity.infectionId = XmlConvert.ToInt64(reader.ReadElementString());
                        break;
                    case "StoneRemain":
                        entity.stoneRemain = reader.ReadElementString();
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                writer.WriteElementString("SurgeryDate", XmlConvert.ToString(entity.SurgeryDate, XmlDateTimeSerializationMode.Utc));
                if (entity.KidneyId != null) writer.WriteElementString("KidneyId", XmlConvert.ToString(entity.KidneyId.Value));
                if (entity.SoftTubeId != null) writer.WriteElementString("SoftTubeId", XmlConvert.ToString(entity.SoftTubeId.Value));
                if (entity.HardTubeId != null) writer.WriteElementString("HardTubeId", XmlConvert.ToString(entity.HardTubeId.Value));
                if (entity.SpermId != null) writer.WriteElementString("SpermId", XmlConvert.ToString(entity.SpermId.Value));
                if (entity.PgId != null) writer.WriteElementString("PgId", XmlConvert.ToString(entity.PgId.Value));
                if (entity.KidneyPos != null) writer.WriteElementString("KidneyPos", XmlConvert.ToString(entity.KidneyPos.Value));
                if (entity.StoneSize != null) writer.WriteElementString("StoneSize", XmlConvert.ToString(entity.StoneSize.Value));
                if (entity.StonePosition != null) writer.WriteElementString("StonePosition", entity.StonePosition);
                if (entity.RecordFileId != null) writer.WriteElementString("RecordFileId", XmlConvert.ToString(entity.RecordFileId.Value));
                if (entity.InfectionId != null) writer.WriteElementString("InfectionId", XmlConvert.ToString(entity.InfectionId.Value));
                if (entity.StoneRemain != null) writer.WriteElementString("StoneRemain", entity.StoneRemain);
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<Surgery>
        {
            Surgery entity;
            public DefaultJsonSerializer(Surgery entity)
                : base()
            {
                this.entity = entity;
            }

            public Surgery Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "surgeryDate":
                        entity.surgeryDate = DbConvert.ToNullableDateTime(propValue) ?? entity.surgeryDate;
                        break;
                    case "kidneyId":
                        entity.kidneyId = DbConvert.ToNullableInt64(propValue) ?? entity.kidneyId;
                        break;
                    case "softTubeId":
                        entity.softTubeId = DbConvert.ToNullableInt64(propValue) ?? entity.softTubeId;
                        break;
                    case "hardTubeId":
                        entity.hardTubeId = DbConvert.ToNullableInt64(propValue) ?? entity.hardTubeId;
                        break;
                    case "spermId":
                        entity.spermId = DbConvert.ToNullableInt64(propValue) ?? entity.spermId;
                        break;
                    case "pgId":
                        entity.pgId = DbConvert.ToNullableInt64(propValue) ?? entity.pgId;
                        break;
                    case "kidneyPos":
                        entity.kidneyPos = DbConvert.ToNullableInt32(propValue) ?? entity.kidneyPos;
                        break;
                    case "stoneSize":
                        entity.stoneSize = DbConvert.ToNullableDecimal(propValue) ?? entity.stoneSize;
                        break;
                    case "stonePosition":
                        entity.stonePosition = Convert.ToString(propValue) ?? entity.stonePosition;
                        break;
                    case "recordFileId":
                        entity.recordFileId = DbConvert.ToNullableInt64(propValue) ?? entity.recordFileId;
                        break;
                    case "infectionId":
                        entity.infectionId = DbConvert.ToNullableInt64(propValue) ?? entity.infectionId;
                        break;
                    case "stoneRemain":
                        entity.stoneRemain = Convert.ToString(propValue) ?? entity.stoneRemain;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //手术时间
                writer.WritePropertyName("surgeryDate");
                writer.WriteValue(entity.SurgeryDate);

                //经皮肾镜
                writer.WritePropertyName("kidneyId");
                if (entity.KidneyId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.KidneyId.Value);
                }

                //输尿软管镜
                writer.WritePropertyName("softTubeId");
                if (entity.SoftTubeId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.SoftTubeId.Value);
                }

                //输尿硬管镜
                writer.WritePropertyName("hardTubeId");
                if (entity.HardTubeId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.HardTubeId.Value);
                }

                //精囊镜
                writer.WritePropertyName("spermId");
                if (entity.SpermId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.SpermId.Value);
                }

                //前列腺手术
                writer.WritePropertyName("pgId");
                if (entity.PgId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.PgId.Value);
                }

                //肾的左右
                writer.WritePropertyName("kidneyPos");
                if (entity.KidneyPos == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.KidneyPos.Value);
                }

                //结石大小
                writer.WritePropertyName("stoneSize");
                if (entity.StoneSize == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.StoneSize.Value);
                }

                //结石位置
                writer.WritePropertyName("stonePosition");
                if (entity.StonePosition == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.StonePosition);
                }

                //手术录像文件id
                writer.WritePropertyName("recordFileId");
                if (entity.RecordFileId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.RecordFileId.Value);
                }

                //感染
                writer.WritePropertyName("infectionId");
                if (entity.InfectionId == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.InfectionId.Value);
                }

                //结石残留
                writer.WritePropertyName("stoneRemain");
                if (entity.StoneRemain == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.StoneRemain);
                }

                //备注
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //更新时间戳
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}